package cc.jorange.common.helper;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.google.common.collect.Lists;

/**
 * 时间处理
 * 
 * @author Aaron
 *
 */
public class LogHelper {
	
	public static void main(String[] args) {
		
	}

	/**
	 * <p>Discription:[根据参数获取自定义事件]</p> 
	 * @param day 0 为当天时间 -1为前天  1为后天
	 * @return
	 */
	public static long getCustomToday(int day) {
		Calendar nowDate = new java.util.GregorianCalendar();
		nowDate.set(Calendar.HOUR_OF_DAY, 0);
		nowDate.set(Calendar.MINUTE, 0);
		nowDate.set(Calendar.SECOND, 0);
		nowDate.set(Calendar.MILLISECOND, 0);
		nowDate.add(Calendar.DAY_OF_YEAR, day);
		//return nowDate.getTimeInMillis() / 1000;
		return nowDate.getTimeInMillis();
	}
	/**
	 * <p>Discription:[把String时间格式化成时间戳]</p> 
	 * @param day
	 * @param format
	 * @return
	 * @throws ParseException 
	 */
	public static long getCustomStringDateToLong(String day, String pattern) throws ParseException {
		if (StringUtils.isEmpty(pattern))
			pattern = "dd/MM/yyyy";
		SimpleDateFormat sdf=new SimpleDateFormat(pattern);
		Date dt = sdf.parse(day);
		return dt.getTime(); 
	}

	/**
	 * <p>Discription:[格式化时间戳，参数为秒，不需要乘以1000]</p> 
	 * @param pattern 输出格式
	 * @param date    时间戳
	 * @return
	 */
	public static String formatTimeStamp(String pattern, long date) {
		if(StringUtils.isEmpty(pattern))
			pattern = "yyyy-MM-dd HH:mm:ss";
		java.util.Calendar nowDate = new java.util.GregorianCalendar();
		nowDate.setTimeInMillis(date);
		DateFormat df = new SimpleDateFormat(pattern);
		return df.format(nowDate.getTime());
	}
	
	public static String formatTimeStamp(String pattern, Date date) {
		if(StringUtils.isEmpty(pattern))
			pattern = "yyyy-MM-dd HH:mm:ss";
		DateFormat df = new SimpleDateFormat(pattern);
		return df.format(date);
	}
	
	/**
	 * <p>Discription:[为KEY附加时间戳]</p> 
	 * @param keyName key值
	 * @return install&1340640000000
	 */
	public static String getDayKey(String keyName,int hour) {
		StringBuffer sb = new StringBuffer();
		sb.append(keyName);
		sb.append(":");
		sb.append(getCustomToday(hour));
		return sb.toString();
	}
	
	/**
	 * <p>Discription:[根据参数获取自定义事件]</p> 
	 * @param day 00 为当天时间 -1为前天  1为后天
	 * @return
	 */
	public static long getHourToday(int hour, int day) {
		Calendar nowDate = new java.util.GregorianCalendar();
		nowDate.set(Calendar.HOUR_OF_DAY, hour);
		nowDate.set(Calendar.MINUTE, 0);
		nowDate.set(Calendar.SECOND, 0);
		nowDate.set(Calendar.MILLISECOND, 0);
		nowDate.add(Calendar.DAY_OF_YEAR, day);
		//return nowDate.getTimeInMillis() / 1000;
		return nowDate.getTimeInMillis();
	}
	
	public static List<Long> getHourTodayList(int day, String date) {
		List<Long> list = Lists.newArrayList();
		for(int i = 0; i <= 24; i++) {
			list.add(LogHelper.getHourToday(i, day));
		}
		return list;
	}
	
	/**
	 * <p>Discription:[格式化系统时间戳，天]</p> 
	 * @param date 系统时间戳
	 * @return 去掉小时之后的时间戳
	 */
	public static Long formatTimeStamp2DayLong(long timestamp) {
		java.util.Calendar nowDate = new java.util.GregorianCalendar();
		nowDate.setTimeInMillis(timestamp);
		nowDate.set(Calendar.HOUR_OF_DAY, 0);
		nowDate.set(Calendar.MINUTE, 0);
		nowDate.set(Calendar.SECOND, 0);
		nowDate.set(Calendar.MILLISECOND, 0);
		nowDate.add(Calendar.DAY_OF_YEAR, 0);
		return nowDate.getTimeInMillis();
	}
	
	/**
	 * <p>Discription:[格式化系统时间戳，小时]</p> 
	 * @param date 系统时间戳
	 * @return 独去掉分之后的时间戳
	 */
	public static Long formatTimeStamp2HourLong(long timestamp) {
		java.util.Calendar nowDate = new java.util.GregorianCalendar();
		nowDate.setTimeInMillis(timestamp);
		nowDate.set(Calendar.MINUTE, 0);
		nowDate.set(Calendar.SECOND, 0);
		nowDate.set(Calendar.MILLISECOND, 0);
		nowDate.add(Calendar.DAY_OF_YEAR, 0);
		return nowDate.getTimeInMillis();
	}
	
	/**
	 * <p>Discription:[计算百分比]</p> 
	 * @param y
	 * @param z
	 * @return
	 */
	public static Double getPercent(double p1, double p2) {
		if(p2==0){
			return 0.0;	
		}else{
			String baifenbi = "";
			double p3 = p1 / p2 * 100;
			DecimalFormat df1 = new DecimalFormat("##.#"); 
			baifenbi = df1.format(p3);
			return Double.parseDouble(baifenbi);
		}
	}
	
	/**
	 * <p>Discription:[计算百分比]</p> 
	 * @param y
	 * @param z
	 * @return
	 */
	public static String percent(double p1, double p2) {
		String str ="0%";
		double p3 = 0;
		if(p2 > 0) {
			p3 = p1 / p2;
			DecimalFormat nf = new DecimalFormat("0%");
			nf.setMinimumFractionDigits(2);
			str = nf.format(p3);
		}
		return str;
	}
	
	/**
	 * 保留两位小数
	 * 
	 * @param number
	 * @return
	 */
	public static String decimalFormat(double number) {
		DecimalFormat df = new DecimalFormat("#.00");
        return df.format(number);
	}
	
	/**
	 * 把当前时间戳加上一天
	 * 
	 * @param timestamp
	 * @param increment
	 * @return
	 */
	public static Long formatTimeStamp2DayIncrement(long timestamp, int increment) {
		java.util.Calendar nowDate = new java.util.GregorianCalendar();
		nowDate.setTimeInMillis(timestamp);
		nowDate.set(Calendar.HOUR_OF_DAY, 0);
		nowDate.set(Calendar.MINUTE, 0);
		nowDate.set(Calendar.SECOND, 0);
		nowDate.set(Calendar.MILLISECOND, 0);
		nowDate.add(Calendar.DAY_OF_YEAR, increment);
		return nowDate.getTimeInMillis();
	}
	
	/**
	 * <p>Discription:[根据参数时间间隔循环出所对应的时间]</p>
	 * @param startTime 字符串类型起始时间 2012-10-23
	 * @param endTime 字符串类型结束时间 2012-10-24
	 * @param pattern 格式化 "yyyy-MM-dd"
	 * @return List<Long> 返回的布尔值 
	 */
	public static List<Long> getTimes(String startTime, String endTime, String pattern) {
		try {
			List<Long> timeList = Lists.newArrayList();
			if (StringUtils.isEmpty(pattern))
				pattern = "yyyy-MM-dd";
			long startTimeToLong = LogHelper.getCustomStringDateToLong(startTime, pattern);
			long endTimeToLong = LogHelper.getCustomStringDateToLong(endTime, pattern);
			long incrementTime = startTimeToLong;
			if(startTimeToLong < endTimeToLong) {
				timeList.add(startTimeToLong);
				while(incrementTime != endTimeToLong) {
					incrementTime = LogHelper.formatTimeStamp2DayIncrement(incrementTime,1);
					timeList.add(incrementTime);
				} 
				return timeList;
			} else if (startTimeToLong == endTimeToLong) {
				timeList.add(incrementTime);
				return timeList;
			} else {
				return null;
			}
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static List<String> getTimesString(String startTime, String endTime, String pattern) {
		try {
			List<String> timeList = Lists.newArrayList();
			if (StringUtils.isEmpty(pattern))
				pattern = "yyyy-MM-dd";
			long startTimeToLong = LogHelper.getCustomStringDateToLong(startTime, pattern);
			long endTimeToLong = LogHelper.getCustomStringDateToLong(endTime, pattern);
			long incrementTime = startTimeToLong;
			if(startTimeToLong < endTimeToLong) {
				timeList.add(LogHelper.formatTimeStamp(pattern, startTimeToLong));
				while(incrementTime != endTimeToLong) {
					incrementTime = LogHelper.formatTimeStamp2DayIncrement(incrementTime,1);
					timeList.add(LogHelper.formatTimeStamp(pattern, incrementTime));
				} 
				return timeList;
			} else if (startTimeToLong == endTimeToLong) {
				timeList.add(LogHelper.formatTimeStamp(pattern, incrementTime));
				return timeList;
			} else {
				return null;
			}
		} catch (ParseException e) {
			    e.printStackTrace();
			    return null;
		}
	}
	
	public static long addOneDay(String day, String pattern) throws ParseException {
		long endTimeToLong = LogHelper.getCustomStringDateToLong(day, pattern);
		return LogHelper.formatTimeStamp2DayIncrement(endTimeToLong,1);
	}
	
	/**
	 * <p>Discription:[根据时间数字输出从当天起之前时间]</p> 
	 *@param date
	 *@return
	 */
	public static List<Long> getTimes(int date) {
		List<Long> timeList = Lists.newArrayList();
		for(int i = -date+1; i <= 0; i ++) {
			timeList.add(LogHelper.getCustomToday(i));
		}
		return timeList;
	}
	
	/**
	 * <p>Discription:[根据参数输出查询日志字符串]</p> 
	 * @param start
	 * @param end
	 * @return
	 */
	public static List<String> getDateList(List<Long> times) {
		List<String> dateList = Lists.newArrayList();
		for(long time : times){
			dateList.add(LogHelper.formatTimeStamp("yyyy-MM-dd", time));
			//dateList.add(LogHelper.formatTimeStamp("yyyy-MM-dd", time));
		}
		return dateList;
	}
	
	/**
	 * 比较时间间隔天数
	 * 
	 * @param end
	 * @param start
	 * @return
	 */
	public static int daysBetween(Date end, Date start) {
		Calendar cNow = Calendar.getInstance();
		Calendar cReturnDate = Calendar.getInstance();
		cNow.setTime(end);
		cReturnDate.setTime(start);
		long todayMs = cNow.getTimeInMillis();
		long returnMs = cReturnDate.getTimeInMillis();
		long intervalMs = todayMs - returnMs;
		return millisecondsToDays(intervalMs);
	}

	private static int millisecondsToDays(long intervalMs) {
		return (int) (intervalMs / (1000 * 86400));
	}
	
	public static boolean checkTimeSpan(String startTime, String endTime, String pattern) {
		boolean resultflag = false;
		long startTimeToLong;
		try {
			if (StringUtils.isEmpty(pattern))
				pattern = "yyyy-MM-dd";
			startTimeToLong = LogHelper.getCustomStringDateToLong(startTime, pattern);
			long endTimeToLong = LogHelper.getCustomStringDateToLong(endTime, pattern);
			long resultTime = endTimeToLong - startTimeToLong;
			long flag = 7862400000L;
			if(resultTime < flag) {
				resultflag = true;
			}
		}
		catch (ParseException e) {
			return resultflag;
		}
		return resultflag;
	}
	
	/**
	 * 判断是否是周日
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static boolean isWeekend(String date, String pattern) {
		boolean flag =  false;
		try {
			SimpleDateFormat df = new SimpleDateFormat(pattern);
			Date time = df.parse(date);
			Calendar cal = Calendar.getInstance();
			cal.setTime(time);
			int day = cal.get(Calendar.DAY_OF_WEEK);
			if (day == Calendar.SUNDAY) {
				flag =  true;
			}
		}
		catch (ParseException e) {
		}
		return flag;
	}
	
	public static boolean isWeekend(Date date) {
		boolean flag = false;
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
			flag = true;
		}
		return flag;
	}
	
	public static boolean isMonday(Date date) {
		boolean flag = false;
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
			flag = true;
		}
		return flag;
	}
	
	/**
	 * 返回上周一
	 * 
	 * @return
	 */
	public static Date getModayOfWeek() {
		Calendar c = Calendar.getInstance();
		int dayOfweek = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (dayOfweek == 0)
			dayOfweek = 7;
		c.add(java.util.Calendar.DATE, -(dayOfweek - 1) - 7);
		return c.getTime();
	}

	/**
	 * 返回上周日，如果N=0返回上周日， n>0 对应上周日加上N天
	 * 
	 * @param n
	 * @return
	 */
	public static Date getSundayOfThisWeek(int n) {
		Calendar c = Calendar.getInstance();
		int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (day_of_week == 0)
			day_of_week = 7;
		c.add(Calendar.DATE, -day_of_week + n);
		return c.getTime();
	}
	
}
